﻿using System;
using System.Xml;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Web.Services;
using System.Net;
using File_Upload_Tool.localhost;

namespace File_Upload_Tool
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

        }

        private void btnFileSelect_Click(object sender, EventArgs e)
        {

            if (txtInvestigatorID.Text != "" && txtCrimeSceneID.Text != "")
            {
                openFD.Title = "Select File For Upload";
                openFD.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                openFD.ShowDialog();

                txtFileName.Text = openFD.FileName;
                
                txtMD5.Text = GetMD5HashFromFile(openFD.FileName);
            } else if (txtInvestigatorID.Text == "")
            {
                MessageBox.Show("Please Enter your ID number");
            }
            else
            {
                MessageBox.Show("Please Enter the crime scene ID");
            }
        }

        private string xml_generator()
        {
           string xmlFileName = Path.GetFileNameWithoutExtension(txtFileName.Text) + "-META.xml";
           
            Metadata met = new Metadata();
            Uploader xmlW = new Uploader();
            
            met.filename = this.txtFileName.Text;
            met.crimeSceneID = this.txtCrimeSceneID.Text;
            met.investigatorID = this.txtInvestigatorID.Text;
            met.sha1 = this.txtMD5.Text;
            
            string d = xmlW.SetMeta(xmlFileName, met);

            return d;

        }

        protected string GetMD5HashFromFile(string fileName)
        {
            using (FileStream MD5Stream = new FileStream(fileName, FileMode.Open))
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(MD5Stream);
                
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {

            string successUpload = UploadFile(this.txtFileName.Text);
            string successXML = xml_generator();

            MessageBox.Show(successUpload + successXML);

        }


        private string UploadFile(string filename)
        {

            try
            {

                String strFile = System.IO.Path.GetFileName(filename);

                File_Upload_Tool.localhost.Uploader upload = new File_Upload_Tool.localhost.Uploader();

                FileInfo fInfo = new FileInfo(filename);

                long numBytes = fInfo.Length;

                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                if (dLen < 4)
                {

                    using (FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
                    {
                        using (BinaryReader br = new BinaryReader(fStream))
                        {
                            byte[] data = br.ReadBytes((int)numBytes);
                            
                            string sTmp = upload.UploadFile(data, strFile);
                            return "success";
                           // MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
                        }
                    }
                }
                else
                {
                    return "The file selected exceeds the size limit for uploads.";
                }
            }
            catch (Exception ex)
            {
                return "Upload Error "+ ex;
            }
        }

    }
}

